package com.jiantong.web.open.app.work;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.QuarantineCenterHarmless;
import com.jiantong.work.service.QuarantineCenterHarmlessReadService;
import com.jiantong.work.service.QuarantineCenterHarmlessWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * Created by niufei on 2017/3/30.
 *
 */
@Slf4j
@OpenBean
public class QuarantineCenterHarmlessApis {

    @RpcConsumer
    private QuarantineCenterHarmlessReadService quarantineCenterHarmlessReadService;

    @RpcConsumer
    private QuarantineCenterHarmlessWriteService quarantineCenterHarmlessWriteService;

    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @OpenMethod(
            key = "find.quarantine.center.harmless.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public QuarantineCenterHarmless findQuarantineCenterHarmlessById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"id should be not null");
        }
        Response<QuarantineCenterHarmless> response = quarantineCenterHarmlessReadService.findQuarantineCenterHarmlessById(id);
        if(!response.isSuccess()){
            log.error("failed to find quarantineCenterHarmless , cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "paging.quarantine.harmless",
            paramNames = {"pageNo","pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<QuarantineCenterHarmless> pagingQuarantineCenterHarmlessFarm(Integer pageNo, Integer pageSize,
                                                                               Integer status, Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId is not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId is not null");
        }
        Response<Paging<QuarantineCenterHarmless>> response =
                quarantineCenterHarmlessReadService.pagingQuarantineCenterHarmless(pageNo,pageSize,status,breedFactoryId);
        if(!response.isSuccess()){
            log.error("failed to paging quarantineCenterHarmless farm , cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "create.quarantine.center.harmless",
            paramNames = {"quarantineCenterHarmless","toStatus"},
            httpMethods = RequestMethod.POST
    )
    public Map<String , Object> createQuarantineCenterHarmless(QuarantineCenterHarmless quarantineCenterHarmless , Integer toStatus){

        Response<Map<String , Object>> response = quarantineCenterHarmlessWriteService.createQuarantineCenterHarmless(quarantineCenterHarmless,toStatus);
        if(!response.isSuccess()){
            log.error("failed to create quarantineCenterHarmless, cause : {}",quarantineCenterHarmless,toStatus,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "update.quarantine.center.harmless",
            paramNames = {"quarantineCenterHarmless","toStatus"},
            httpMethods = RequestMethod.PUT
    )
    public  Map<String , Object> updateQuarantineCenterHarmless(QuarantineCenterHarmless quarantineCenterHarmless , Integer toStatus){
        if(quarantineCenterHarmless.getId() == null){
            log.error("failed to update quarantineCenterHarmless error = {}, cause : {}", quarantineCenterHarmless,quarantineCenterHarmless.getId());
            throw new OPClientException("quarantineCenterHarmless.id.is.null");
        }

        Response<Map<String , Object>> statusResponse = quarantineCenterHarmlessReadService.findStatusById(quarantineCenterHarmless.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(quarantineCenterHarmless.getStatus())){
            log.error("quarantineCenterHarmless is already submitted error = {}, cause : {}", quarantineCenterHarmless.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException("this.report.is.already.submitted");
        }

        Response<Map<String , Object>> response = quarantineCenterHarmlessWriteService.updateQuarantineCenterHarmless(quarantineCenterHarmless,toStatus);
        if(!response.isSuccess()){
            log.error("failed to update quarantineCenterHarmless error = {}, cause : {}", quarantineCenterHarmless, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 政府端时间查询
     *
     * @return status =1
     */
    @OpenMethod(
            key = "paging.status.harmless.treatment",
            paramNames = {"pageNo", "pageSize", "breedFactoryId","breedType", "fromDate", "toDate"},
            httpMethods = RequestMethod.GET
    )
    public Paging<QuarantineCenterHarmless> pagingQuarantineCenterHarmless(
                                                            Integer pageNo,
                                                            Integer pageSize,
                                                            Long breedFactoryId,
                                                            Integer breedType,
                                                            String fromDate,
                                                            String toDate) {

        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<QuarantineCenterHarmless>> resp =
                quarantineCenterHarmlessReadService.pagingQuarantineCenterHarmlessGqv(pageNo, pageSize, breedFactoryId,breedType, start, end);
        if (!resp.isSuccess()) {
            log.error("failed to paging quarantine center{}, cause : {}", resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }

}
